
package avenue;

import model.AppUser;
import model.RolePermission;

/**
 *
 * @author Team-2
 * 
 */
public class Session {
    public static AppUser loginUser;
    //Modules
    public static final int USERS_MODULE = 1;
    public static final int CUSTOMERS_MODULE = 2;
    public static final int PROGRAMS_MODULE = 3;
    public static final int TOLL_COLLECTION_MODULE = 4;
    public static final int PAYMENTS_MODULE = 5;
    public static final int LOG_MODULE = 6;
    public static final int ROLES_MODULE = 7;
    //Permission type
    public static final int CAN_CREATE = 1;
    public static final int CAN_RETRIEVE = 2;
    public static final int CAN_UPDATE = 3;
    public static final int CAN_DELETE = 4;
    //icons path
    public final static String imgPath = System.getProperty("user.dir") + "\\src\\images\\";    
    
    public Session() {
    }
    
    public static boolean doesSessionUserHasPermission(int permission){
        boolean ret=false;
        if (loginUser.getId()==null){ 
            return false;
        }
        if (loginUser.getRoleId()==null){ 
            return false;
        }
        if (loginUser.getRoleId().getId()==1){ //administrator (access to all)
            return true;
        }
        if (loginUser.getRoleId().getRolePermissionList()==null){
            return false;
        }
               
        for (RolePermission rp : loginUser.getRoleId().getRolePermissionList()){
            if (rp.getModuleId().getId() == permission && 
                rp.getStationLevelAccess() != null &&
                rp.getStationLevelAccess() == 1)       {

                return true;

             }            
                
        }
        return ret;
    }

  /**
   * Check if given type of permission is enabled
   * @param permission
   * @param type
   * @return 
   */
  public static boolean doesSessionUserHasPermissionType(int permission, int type) {
      
    boolean ret=false;
       
        if (loginUser.getId()==null){ 
            return false;
        }
        if (loginUser.getRoleId()==null){ 
            return false;
        }
        if (loginUser.getRoleId().getId()==1){ //administrator (access to all)
            return true;
        }
        if (loginUser.getRoleId().getRolePermissionList()==null){
            return false;
        }
               
        for (RolePermission rp : loginUser.getRoleId().getRolePermissionList()){
            switch (type) {
                case CAN_CREATE:           
                        if (rp.getModuleId().getId() == permission && 
                            rp.getCanCreate() != null &&
                            rp.getCanCreate() == 1)       {
                            return true;
                         }    
                       break;
                case CAN_RETRIEVE: 
                      if (rp.getModuleId().getId() == permission && 
                            rp.getCanRetrieve() != null &&
                            rp.getCanRetrieve() == 1)       {
                            return true;
                         }  
                       break;
                case CAN_UPDATE: 
                      if (rp.getModuleId().getId() == permission && 
                            rp.getCanUpdate() != null &&
                            rp.getCanUpdate() == 1)       {
                            return true;
                         } 
                      break;
                case CAN_DELETE: 
                      if (rp.getModuleId().getId() == permission && 
                            rp.getCanDelete() != null &&
                            rp.getCanDelete() == 1)       {
                            return true;
                         } 
                       break;
            } 
            
        }
      return ret;
      
  }  
    
}
